package com.example.testrxjava;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.example.testrxjava.base.BaseActivity;
import com.example.testrxjava.util.CommUtil;
import com.example.testrxjava.util.ConTract;
import com.just.agentweb.AgentWeb;
import com.just.agentweb.DefaultWebClient;

import butterknife.BindView;

public class ArticleDetailsActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.containerFrameLayout)
    FrameLayout mContainerFrameLayout;
    @BindView(R.id.webLinear)
    LinearLayout mWebLinear;


    private String mUrl;
    private String mTitle;
    private int mId;
    private boolean isCollect;
    private boolean isCollectHide;
    private MenuItem mCollectionItem;
    private AgentWeb mAgentWeb;

    @Override
    public int getLayoutId() {
        return R.layout.activity_article_details;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void init(Bundle savedInstanceState) {
        getBundleData();
        mToolbar.setTitle(mTitle);
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
//        mToolbar.setNavigationOnClickListener(v -> {
//            Intent intent = new Intent();
//            intent.putExtra(Constant.KEY_ARTICLE_COLLECT, isCollect);
//            setResult(RESULT_OK, intent);
//            finish();
//        });
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
            intent.putExtra(ConTract.KEY_ARTICLE_COLLECT, isCollect);
            setResult(RESULT_OK, intent);
            finish();
            }
        });

        initData();
    }

    private void initData() {
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(mContainerFrameLayout, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator(getResources().getColor(R.color.colorPrimary))
                .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)
                .createAgentWeb()
                .ready()
                .go(mUrl);

        WebView mWebView = mAgentWeb.getWebCreator().getWebView();
        WebSettings mSettings = mWebView.getSettings();
        mSettings.setAppCacheEnabled(true);
        mSettings.setDomStorageEnabled(true);
        mSettings.setDatabaseEnabled(true);


//        if(mPresenter.getNoImgStyleState()){
            mSettings.setBlockNetworkImage(false);//false为加载图片
//        }else{
//            mSettings.setBlockNetworkImage(false);
//        }

        //有网络
        mSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        mSettings.setJavaScriptEnabled(true);
        mSettings.setSupportZoom(true);
        mSettings.setBuiltInZoomControls(true);
        //不显示缩放按钮
        mSettings.setDisplayZoomControls(false);
        //设置自适应屏幕，两者合用
        //将图片调整到适合WebView的大小
        mSettings.setUseWideViewPort(true);
        //缩放至屏幕的大小
        mSettings.setLoadWithOverviewMode(true);
        //自适应屏幕
        mSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_article_detail, menu);
        mCollectionItem = menu.findItem(R.id.collectionItem);
        if(isCollectHide) mCollectionItem.setVisible(false);
        else if (isCollect) mCollectionItem.setTitle(getString(R.string.article_un_collect));
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void initLinstener() {

    }

    @Override
    protected void onDestroy() {
        mAgentWeb.getWebLifeCycle().onDestroy();
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();
    }
    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }
    /**
     * 获取传入的数据
     */
    private void getBundleData() {
        mUrl = getIntent().getStringExtra(ConTract.KEY_ARTICLE_URL);
        mTitle = getIntent().getStringExtra(ConTract.KEY_ARTICLE_TITLE);
        mId = getIntent().getIntExtra(ConTract.KEY_ARTICLE_ID, -1);
        isCollect = getIntent().getBooleanExtra(ConTract.KEY_ARTICLE_COLLECT, false);
        isCollectHide = getIntent().getBooleanExtra(ConTract.KEY_ARTICLE_COLLECT_HIDE,false);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.shareItem:
//                shareLink();
                CommUtil.Toast(this,"分享");
                break;
            case R.id.collectionItem:
//                collection();
                CommUtil.Toast(this,"收藏");
                break;
            case R.id.browserItem:
                openArticlesByBrowser();
        }
        return super.onOptionsItemSelected(item);
    }
    /**
     * 使用系统浏览器打开
     */
    private void openArticlesByBrowser() {
        CommUtil.openBrowser(this, mUrl);
    }


    /**
     * 给其他需要传入数据的碎片使用
     *
     * @param activity
     * @param fragment
     * @param url
     * @param title
     */
    public static void startActivityForResultByFragment(Activity activity,
                                                        Fragment fragment,
                                                        String url,
                                                        String title,
                                                        int id,
                                                        boolean isCollect,
                                                        int request) {
        Intent intent = new Intent(activity, ArticleDetailsActivity.class);
        intent.putExtra(ConTract.KEY_ARTICLE_URL, url);
        intent.putExtra(ConTract.KEY_ARTICLE_TITLE, title);
        intent.putExtra(ConTract.KEY_ARTICLE_ID, id);
        intent.putExtra(ConTract.KEY_ARTICLE_COLLECT, isCollect);
        fragment.startActivityForResult(intent,request);
    }

    /**
     * 给其他需要传入数据的碎片使用
     *
     * @param activity
     * @param url
     * @param title
     */
    public static void startActivityForResultByActivity(Activity activity,
                                                        String url,
                                                        String title,
                                                        int id,
                                                        boolean isCollect,
                                                        int request) {
        Intent intent = new Intent(activity, ArticleDetailsActivity.class);
        intent.putExtra(ConTract.KEY_ARTICLE_URL, url);
        intent.putExtra(ConTract.KEY_ARTICLE_TITLE, title);
        intent.putExtra(ConTract.KEY_ARTICLE_ID, id);
        intent.putExtra(ConTract.KEY_ARTICLE_COLLECT, isCollect);
        activity.startActivityForResult(intent,request);
    }

    /**
     * 给没有文章id的调用
     * @param activity
     * @param fragment
     * @param url
     * @param title
     */
    public static void startActivityByFragment(Activity activity, Fragment fragment, String url, String title) {
        Intent intent = new Intent(activity, ArticleDetailsActivity.class);
        intent.putExtra(ConTract.KEY_ARTICLE_URL, url);
        intent.putExtra(ConTract.KEY_ARTICLE_TITLE, title);
        fragment.startActivity(intent);
    }

    //
    public static void startActivityByCollectionActivity(Activity activity, String url, String title) {
        Intent intent = new Intent(activity, ArticleDetailsActivity.class);
        intent.putExtra(ConTract.KEY_ARTICLE_URL, url);
        intent.putExtra(ConTract.KEY_ARTICLE_TITLE, title);
        intent.putExtra(ConTract.KEY_ARTICLE_COLLECT_HIDE,true);
        activity.startActivity(intent);
    }

}
